/**
 * 文件名：ResourceFacadeImpl.java
 *
 * 创建人：Fyh - 1009173473@qq.com
 *
 * 创建时间：2019年1月11日 上午10:49:29
 *
 * 版权所有：南京植百汇智能科技服务有限公司
 */
package com.z100h.supplychain.core.biz.facade.impl;

import java.util.ArrayList;
import java.util.List;

import org.activiti.editor.language.json.converter.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.jeeplus.core.persistence.Page;
import com.z100h.supplychain.core.biz.entity.ResourceDetail;
import com.z100h.supplychain.core.biz.facade.ResourceFacade;
import com.z100h.supplychain.core.biz.service.ResourceService;

/**
 * [描述信息：说明类的基本功能]
 *
 * @author Fyh - 1009173473@qq.com
 * @version 1.0 Created on 2019年1月11日 上午10:49:29
 */
@Service
public class ResourceFacadeImpl implements ResourceFacade {

	@Autowired
	private ResourceService resourceService;
	
	public static final String RESOURCE_STATE_NORMAL = "正常";
	
	public static final String RESOURCE_STATE_ABNORMAL = "异常";
	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.core.biz.facade.ResourceFacade#findResourceDetailByIds(java.util.List)
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年1月11日 上午10:51:38
	 */
	@Override
	public List<ResourceDetail> findResourceDetailByIds(List<String> ids) {
		List<ResourceDetail> resourceList=new ArrayList<ResourceDetail>();
		ResourceDetail resourceDetail=null;
		if(CollectionUtils.isNotEmpty(ids)) {
			resourceList=resourceService.findResourceDetailByIds(ids);
			//存在的编码
			if(CollectionUtils.isNotEmpty(resourceList)) {
				for(ResourceDetail r:resourceList) {
					r.setState(RESOURCE_STATE_NORMAL);
					ids.remove(r.getId());
				}
			}
		}
		if(CollectionUtils.isNotEmpty(ids)) {
			for(String s:ids) {
				resourceDetail=new ResourceDetail();
				resourceDetail.setId(s);
				resourceDetail.setState(RESOURCE_STATE_ABNORMAL);
				resourceList.add(resourceDetail);
			}
		}
		return resourceList;
	}
	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.core.biz.facade.ResourceFacade#findResourceDetailList(com.jeeplus.core.persistence.Page, com.z100h.supplychain.core.biz.entity.ResourceDetail)
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年1月14日 下午7:18:22
	 */
	@Override
	public Page<ResourceDetail> findResourceDetailList(Page<ResourceDetail> page, ResourceDetail entity) {
		return resourceService.findResourceDetailList(page, entity);
	}

	@Override
	public List<ResourceDetail> checkResourceIdsByStockOut(List<String> ids) {
		List<ResourceDetail> resourceList=new ArrayList<ResourceDetail>();
		ResourceDetail resourceDetail=null;
		if(CollectionUtils.isNotEmpty(ids)) {
			resourceList=resourceService.checkResourceIdsByStockOut(ids);
			//存在的编码
			if(CollectionUtils.isNotEmpty(resourceList)) {
				for(ResourceDetail r:resourceList) {
					r.setState(RESOURCE_STATE_NORMAL);
					ids.remove(r.getId());
				}
			}
		}
		if(CollectionUtils.isNotEmpty(ids)) {
			for(String s:ids) {
				resourceDetail=new ResourceDetail();
				resourceDetail.setId(s);
				resourceDetail.setState(RESOURCE_STATE_ABNORMAL);
				resourceList.add(resourceDetail);
			}
		}
		return resourceList;
	}
	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.core.biz.facade.ResourceFacade#findResourceCountByIds(java.util.List)
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年1月30日 下午4:01:42
	 */
	@Override
	public JSONObject findResourceCountByIds(List<String> ids) {
		//上传编码的总数量
		Integer totalCount=0;
		//正确码的数量
		Integer totalCurrectCount=0;
		//错误码的数量
		Integer totalErrorCount=0;
		
		JSONObject obj=new JSONObject();
		List<ResourceDetail> resourceList=null;
		List<ResourceDetail> resourceCount=null;
		if(CollectionUtils.isNotEmpty(ids)) {
			totalCount=ids.size();
			resourceList=resourceService.findResourceDetailByIds(ids);
			resourceCount=resourceService.findResourceCountByIds(ids);
			//存在的编码
			if(CollectionUtils.isNotEmpty(resourceList)) {
				totalCurrectCount=resourceList.size();
				for(ResourceDetail r:resourceList) {
					ids.remove(r.getId());
				}
			}
		}
		if(CollectionUtils.isNotEmpty(ids)) {
			totalErrorCount=ids.size();
		}
		obj.put("totalErrorCount", totalErrorCount);
		obj.put("totalCount", totalCount);
		obj.put("totalCurrectCount", totalCurrectCount);
		obj.put("resourceList", resourceCount);
		return obj;
	}
}

